package com.druid.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.druid.entity.CeSecurityPermission;
import com.druid.entity.CeSecurityResource;
import com.druid.model.ResponseData;
import com.druid.request.RequestParamInterfaceGroup;
import com.druid.request.SecurityResourceParam;
import com.druid.response.SecurityResourceVm;
import com.druid.service.managementservice.ICeSecurityPermissionService;
import com.druid.service.managementservice.ICeSecurityResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author druid-elf
 * @since 2019-12-25
 */
@Api(tags={"服务器安全保护路径资源表前端控制器"})
@RestController
@RequestMapping("/ceSecurityResource")
@RequiredArgsConstructor
public class CeSecurityResourceController {

    private final ICeSecurityResourceService iCeSecurityResourceService;
    private final ICeSecurityPermissionService iCeSecurityPermissionService;

    @ApiOperation("得到服务器安全保护路径资源表分页列表")
    @GetMapping(value = "getCeSecurityResourceList")
    public ResponseData<List<SecurityResourceVm>> getCeSecurityResourceList() {

        //  获取权限列表
        List<CeSecurityPermission> permissionList = iCeSecurityPermissionService.list();
        //  获取权限资源列表前端返回视图
        List<SecurityResourceVm> collect = permissionList.stream().map(o -> {

            //  获取权限对应的资源列表
            List<CeSecurityResource> list = iCeSecurityResourceService.list(new LambdaQueryWrapper<CeSecurityResource>().eq(CeSecurityResource::getPermissionId,o.getId()));

            return SecurityResourceVm
                    .builder()
                    .permissionName(o.getName())
                    .permissionId(o.getId())
                    .resourceList(list)
                    .build();
        }).collect(Collectors.toList());
        return ResponseData.SUCCESS( collect );
    }

    @ApiOperation("添加安全资源路径")
    @PostMapping("addSecutityResrouce")
    public ResponseData<String> addSecutityResrouce (@RequestBody @Validated(RequestParamInterfaceGroup.InsertGroup.class) SecurityResourceParam securityResourceParam ) {

        CeSecurityResource ceSecurityResource = CeSecurityResource
                .builder()
                .name( securityResourceParam.getName() )
                .url( securityResourceParam.getUrl() )
                .permissionId( securityResourceParam.getPermissionId() )
                .build();

        return iCeSecurityResourceService.save( ceSecurityResource )
                ? ResponseData.SUCCESS("操作成功")
                : ResponseData.FAILURE("操作失败");
    }

    @ApiOperation("删除安全资源路径")
    @PostMapping("deleteSecutityResrouce")
    public ResponseData<String> deleteSecutityResrouce (@RequestBody @Validated(RequestParamInterfaceGroup.DeleteGroup.class) SecurityResourceParam securityResourceParam ) {

        return iCeSecurityResourceService.removeById( securityResourceParam.getId() )
               ? ResponseData.SUCCESS("操作成功")
               : ResponseData.FAILURE("操作失败");
    }
}
